package com.duowei.diandan.adapter;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.duowei.diandan.R;
import com.duowei.diandan.bean.food.MealChildChecked;
import com.duowei.diandan.databinding.MealCheckedItemBinding;

import java.util.List;

public class MealCheckedAdapter extends RecyclerView.Adapter<MealCheckedAdapter.ViewHolder> {

    private List<MealChildChecked> mList;

    public MealCheckedAdapter(List<MealChildChecked> list) {
        mList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MealCheckedItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.meal_checked_item, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MealChildChecked item = mList.get(holder.getAdapterPosition());
        item.setOnDataChangedListener(() -> notifyItemChanged(holder.getAdapterPosition()));

        holder.mBinding.setItem(item);
        holder.mBinding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        MealCheckedItemBinding mBinding;

        public ViewHolder(MealCheckedItemBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }
    }
}
